package service

import (
	"context"
	"video-srv/basic/config"
	__ "video-srv/basic/proto/rpc"
	"video-srv/handler/model"
)

func (s *VideoServer) GetEpisodeById(_ context.Context, in *__.GetEpisodeByIdReq) (*__.GetEpisodeByIdResp, error) {
	var episode model.Episode
	list, err := episode.GetEpisodeById(config.DB, int(in.EpisodeId))
	if err != nil {
		return nil, err
	}
	return &__.GetEpisodeByIdResp{
		List: list,
	}, nil
}
func (s *VideoServer) GetEpisodeList(_ context.Context, in *__.GetEpisodeListReq) (*__.GetEpisodeListResp, error) {
	var episode model.Episode
	err := episode.GetEpisodeList(config.DB, int(in.VideoId))
	if err != nil {
		return nil, err
	}
	log := model.EpisodeLog{
		VideoId:      int(in.VideoId),
		EpisodeOrder: episode.EpisodeOrder,
		Duration:     episode.Duration,
	}
	err = log.CreateEpisodeLog(config.DB)
	if err != nil {
		return nil, err
	}
	return &__.GetEpisodeListResp{
		Title:        episode.Title,
		Duration:     int64(episode.Duration),
		EpisodeOrder: int64(episode.EpisodeOrder),
		Refer:        episode.Refer,
		AuthEr:       episode.AuthEr,
		LookCount:    int64(episode.LookCount),
		LikeCount:    int64(episode.LikeCount),
		CommentCount: int64(episode.CommentCount),
		CollectCount: int64(episode.CollectCount),
		IsFree:       int64(episode.IsFree),
	}, nil
}

func (s *VideoServer) LookEpisode(_ context.Context, in *__.LookEpisodeReq) (*__.LookEpisodeResp, error) {
	log := model.EpisodeLog{
		VideoId:      int(in.VideoId),
		EpisodeOrder: int(in.EpisodeOrder),
		Duration:     int(in.Duration),
	}
	err := log.CreateEpisodeLog(config.DB)
	if err != nil {
		return nil, err
	}
	return &__.LookEpisodeResp{
		LookEpisodeId: int64(log.ID),
	}, nil
}

func (s *VideoServer) GetEpisodeLogList(_ context.Context, in *__.GetEpisodeLogListReq) (*__.GetEpisodeLogListResp, error) {
	var log model.EpisodeLog
	list, err := log.GetEpisodeLogList(config.DB, int(in.Page), int(in.Size))
	if err != nil {
		return nil, err
	}
	return &__.GetEpisodeLogListResp{
		List: list,
	}, nil
}
